﻿Imports DTO
Public Class NhaCungCapDAO
    Public Shared Function DanhSach_ngonngu_DAO(ByVal strSQL As String) As List(Of NhaCungCapDTO)
        Dim kq As New List(Of NhaCungCapDTO)()
        Dim datatable As DataTable = ketnoi.QR_SQL(strSQL)
        Dim ncc As NhaCungCapDTO
        Dim i = 0
        While (i < datatable.Rows.Count)
            ncc = New NhaCungCapDTO
            ncc.MaNhaCungCap = datatable.Rows(i)("MaNCC").ToString
            ncc.TenNhaCungCap = datatable.Rows(i)("TenNCC").ToString
            ncc.DiaChi = datatable.Rows(i)("DiaChi").ToString
            ncc.SDT = datatable.Rows(i)("SDT").ToString
            kq.Add(ncc)
            i = i + 1
        End While
        Return kq
    End Function
    Public Shared Function Them_Loai_DAO(ByVal ncc As NhaCungCapDTO) As String
        Dim sql = ""
        If ncc.MaNhaCungCap = "" Then
            Return "Lỗi: Chưa Nhập Mã Sách"
        End If
        If ncc.TenNhaCungCap = "" Then
            'sql = "Insert Into LoaiSach(MaLS, TenLS) values('" + loaisach.MaLoaiSach + "',NULL)"
            'Hoặc có thể làm theo dạng:
            sql = String.Format("Insert Into NhaCungCap(MaNCC,TenNCC,DiaChi,SDT) values('{0}',NULL,NULL,NULL)", ncc.MaNhaCungCap)
        Else
            sql = String.Format("Insert Into NhaCungCap(MaNCC,TenNCC,DiaChi,SDT) values('{0}',N'{1}',N'{2}',N'{3}')", ncc.MaNhaCungCap, ncc.TenNhaCungCap, ncc.DiaChi, ncc.SDT)
        End If
        Return ketnoi.NonQR_SQL(sql)
    End Function
    Public Shared Function Sua_Loai_DAO(ByVal ncc As NhaCungCapDTO, ByVal MaCu As String) As String
        Dim sql = ""
        If ncc.MaNhaCungCap = "" Then
            Return "Lỗi: Chưa Nhập Mã Sách"
        End If
        If ncc.TenNhaCungCap = "" Then
            sql = String.Format("Update NhaCungCap set MaNCC='{0}', TenNCC=NULL, DiaChi=NULL, SDT=NULL where MaNCC='{1}'", ncc.MaNhaCungCap, MaCu)
        Else
            sql = String.Format("Update NhaCungCap set MaNCC='{0}', TenNCC=N'{1}', DiaChi=N'{2}', SDT=N'{3}' where MaNCC='{4}'", ncc.MaNhaCungCap, ncc.TenNhaCungCap, ncc.DiaChi, ncc.SDT, MaCu)
        End If
        Return ketnoi.NonQR_SQL(sql)
    End Function
    Public Shared Function Xoa_Loai_DAO(ByVal ncc As NhaCungCapDTO) As String
        Dim sql = "Delete From NhaCungCap where MaNCC='" + ncc.MaNhaCungCap + "'"
        Return ketnoi.NonQR_SQL(sql)
    End Function
End Class
